package com.godz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.godz.mapper.*;
import com.godz.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {
    /**打字动画*/
    final
    indexTwMapper indexTwMapper;
    /**个人介绍*/
    final
    indexPiMapper indexPiMapper;
    /**名人名言*/
    final
    indexSayMapper indexSayMapper;
    /**我的日记*/
    final
    indexMyStoryMapper myStoryMapper;
    /**我的奖项*/
    final
    index2HpMapper index2HpMapper;
    public IndexController(
            indexTwMapper indexTwMapper,
            indexPiMapper indexPiMapper,
            indexSayMapper indexSayMapper,
            indexMyStoryMapper myStoryMapper,
            index2HpMapper index2HpMapper) {
        this.indexTwMapper = indexTwMapper;
        this.indexPiMapper = indexPiMapper;
        this.indexSayMapper = indexSayMapper;
        this.myStoryMapper = myStoryMapper;
        this.index2HpMapper = index2HpMapper;
    }

    @RequestMapping(value = {"/", "/index"})
    public String index(Model model) {
        //打字机动画
        List<indexTw> tws = indexTwMapper.queryAllTw();
        //ES6 阿里巴巴规划 不要用String
        StringBuilder twString = new StringBuilder();
        for (com.godz.pojo.indexTw indexTw : tws) {
            twString.append(indexTw.getTwText()).append(",");
        }
        model.addAttribute("tws", twString);
        //个人信息(这里用的比较麻烦了.是我故意写的,防止忘记底层代码)
        List<indexPi> pis = indexPiMapper.queryAllPi();
        indexPi pi = new indexPi();
        for (com.godz.pojo.indexPi indexPi : pis) {
            pi.setId(indexPi.getId());
            pi.setPiName(indexPi.getPiName());
            pi.setPiPd(indexPi.getPiPd());
            pi.setPiSiFirst(indexPi.getPiSiFirst());
            pi.setPiSiSecond(indexPi.getPiSiSecond());
            break;
        }
        model.addAttribute("Pis", pi);
        //名人名言
        List<indexSay> says = indexSayMapper.queryAllSay();
        model.addAttribute("Says", says);
        //我的日记 最近前三个
        QueryWrapper<indexMyStory> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("story_date").last("limit 3");
        List<indexMyStory> myStories = myStoryMapper.selectList(wrapper);
        model.addAttribute("Story3", myStories);
        //我的奖项
        QueryWrapper<index2Hp> Hps = new QueryWrapper<>();
        Hps.eq("hp_level","国级").orderByDesc("id").last("limit 3");
        List<index2Hp> hpList = index2HpMapper.selectList(Hps);
        model.addAttribute("HpList",hpList);
        return "index";
    }
}
